	package util
{
	import flash.events.MouseEvent;
	
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class ProcessPopupWindow extends TitleWindow
	{
		private var config:ConfigUtil;
		
		private var text:TextInput
		
		private var okBtn:Button
		
		private var cancelBtn:Button;
		
		private var processServlet:String;
		
		private var processId:String;
		
		private var processName:String;
		
		private var packagePath:String;
		
		private var processXML:XML;
		
		private var processTree:Tree;
		
		private var httpService:HTTPService;
		
		[Embed(source="../asserts/folder.gif")]          //这是图片的相对地址
        [Bindable]
        public var folderIcon:Class;
        
        [Embed(source="../asserts/project.gif")]          //这是图片的相对地址
        [Bindable]
        public var projectIcon:Class;
        
        [Embed(source="../asserts/process.gif")]          //这是图片的相对地址
        [Bindable]
        public var processIcon:Class; 
		
		public function ProcessPopupWindow(config:ConfigUtil)
		{
			this.config=config;
			this.layout="absolute";
			
			this.width = 300;
            this.height = 480;
            this.showCloseButton=true;
            this.addEventListener(CloseEvent.CLOSE, onClose);
			processServlet=config.getProcessURL();
			httpService=new HTTPService();
			httpService.url=processServlet;
			httpService.send();
			httpService.resultFormat="xml";
     		httpService.addEventListener(ResultEvent.RESULT,resultHandler);
    		httpService.addEventListener(FaultEvent.FAULT,faultHandler);
    		
			processTree=new Tree();
			
			
            
            processTree.x=0;
            processTree.y=0;
			processTree.width=280;
			processTree.height=370; 
			
			text=new TextInput();
			text.x=processTree.x;
			text.y=processTree.y+processTree.height+10;
			text.width=processTree.width;
			text.height=24;
			
			okBtn=new Button();
			
			okBtn.x=text.x;
			okBtn.y=text.y+text.height+10;
			okBtn.width=80;
			okBtn.height=20;
			okBtn.label="确认";
			
			cancelBtn=new Button();
			
			cancelBtn.x=text.x+text.width-80;
			cancelBtn.y=text.y+text.height+10;
			cancelBtn.width=80;
			cancelBtn.height=20;
			cancelBtn.label="取消";
			
			okBtn.addEventListener(MouseEvent.CLICK, onFinish);
			cancelBtn.addEventListener(MouseEvent.CLICK, onCancel);
			this.addChild(processTree);
			this.addChild(text);
			this.addChild(okBtn);
			this.addChild(cancelBtn);
		}
		
		public function resultHandler(event:ResultEvent){
			trace(event.result.toString());
			
			processXML=new XML(event.result.toString());
			
			var xml:XML=new XML(event.result.toString());
			
			xml=renderXML(xml);
			//delete xml
			
			processTree.dataProvider=xml;
			processTree.showRoot=true;
			processTree.labelField="@name";
			processTree.labelFunction=tree_labelFunc;
			processTree.iconFunction=iconFun;
			processTree.addEventListener(ListEvent.ITEM_CLICK, tree_itemClick);
			//processTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, processSelect);
		}
		
		private function onClose(e:CloseEvent){
			config.setProcessId("");
			PopUpManager.removePopUp(this);
		}
		
		private function onFinish(e:MouseEvent){
			config.setProcessId(this.processId);
			config.setProcessName(this.processName);
			config.setPackagePath(this.packagePath);
			PopUpManager.removePopUp(this);
			new ProcessLoader(config).loader();
		}
		
		private function onCancel(e:MouseEvent){
			config.setProcessId("");
			PopUpManager.removePopUp(this);
		}
		
		private function renderXML(xml:XML):XML{
			var result:String;
			var i:int;
			
			result="<root";
			result+=" name=\"流程列表\"";
			result+=">\n";
			trace(xml.AppSpace);
			trace(xml.SpaceDirectory.toString());
			trace(xml.Processdefinition.toString());
			if(xml.SpaceDirectory.toString()!=""){
				result+=getFolderXML(xml);					
			}
			if(xml.Processdefinition.toString()!=""){
				result+=getProcessXML(xml);	
			}
			result+="</root>\n";
			return new XML(result);

		}
		
		private function getFolderXML(xml:XML):String{
			var result:String="";
			var i:int;
			
			for(i=0;i<xml.SpaceDirectory.length();i++){
					var spaceXML:XML=new XML(xml.SpaceDirectory[i]);
					result+="<folder name=\""+spaceXML.child("name")+"\" >\n";
					if(spaceXML.SpaceDirectory.toString!=""){
						result+=getFolderXML(spaceXML);
					}
					if(spaceXML.Processdefinition.toString!=""){
						result+=getProcessXML(spaceXML);
					}
					result+="</folder>\n";
			}	
			
			return result;
			
		}
		
		private function getProcessXML(xml:XML):String{
			var result:String="";
			var i:int;
			
			for(i=0;i<xml.Processdefinition.length();i++){
					var spaceXML:XML=new XML(xml.Processdefinition[i]);
					result+="<process name=\""+spaceXML.child("name")+"\" id=\""+spaceXML.child("id")+"\" packagePath=\""+spaceXML.child("packagePath")+"\" />\n";					
			}	
			return result;
		}
		
		private function iconFun(item:Object):*{
   			
   			var xml:XML = item as XML;
   			trace(xml.name());
    		if(xml.name().toString()=="root")
     			return projectIcon;
    		else if(xml.name().toString()=="folder")
     			return folderIcon; 
     		else
     			return processIcon;
   		}  
		
		public function faultHandler(event:FaultEvent){
			trace(event.fault);
		}
		
		private function tree_labelFunc(item:XML):String {
			
           //  var children:ICollectionView;
            
             
             if (processTree.dataDescriptor.isBranch(item)) {
             	
               //  children = processTree.dataDescriptor.getChildren(item);
                
                 
             }
             
             return item[processTree.labelField];
             
        }
        
          private function tree_itemClick(evt:ListEvent):void {
                 
              var item:Object = Tree(evt.currentTarget).selectedItem;
              if (processTree.dataDescriptor.isBranch(item)) {
              	
                 processTree.expandItem(item, !processTree.isItemOpen(item), true);
                 
              }else{
              	 var xml:XML = item as XML;	
              	 this.processId=xml.attribute("id").toString();
              	 this.packagePath=xml.attribute("packagePath").toString();
              	 this.processName=xml.attribute("name").toString();;
              	 text.text=this.processName;
              	 config.setProcessId(processId);
              	 config.setProcessName(processName);
              	 config.setPackagePath(packagePath);
              }
           }
           
           private function processSelect(evt:ListEvent):void {
                 
              var item:Object = Tree(evt.currentTarget).selectedItem;
              if (processTree.dataDescriptor.isBranch(item)) {
              	
                 processTree.expandItem(item, !processTree.isItemOpen(item), true);
                 
              }else{
              	 
              	 //this.setVisible(false);
              }
           }
           
           public function getProcessId():String{
           		return this.processId;
           }
           
           public function setPackagePath(v:String){
           		this.packagePath=v;
           }
           
           public function getPackagePath():String{
           		return this.packagePath
           }

	}
}